package com.wen.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wen.mapper.ProductMapper;
import com.wen.mapper.StockDetailsMapper;
import com.wen.pojo.Product;
import com.wen.pojo.StockDetails;
import com.wen.pojo.dto.ProductAndStockDetails;
import com.wen.pojo.dto.ProductDto;
import com.wen.pojo.vo.ProductVo;
import com.wen.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xie
 */
@Slf4j
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private StockDetailsMapper stockDetailsMapper;

    /**
     * 商品信息管理
     *  商品列表 (分页查询)
     * @param dto
     * @return
     */
    @Override
    public Page<Product> queryPage(ProductDto dto, Integer pageNum, Integer size) {

        QueryWrapper<Product> wrapper = new QueryWrapper<Product>();

        if (!StringUtils.isEmpty(dto.getProductName()))
            wrapper.like("productName", dto.getProductName());

        if (!StringUtils.isEmpty(dto.getOnOff()))
            wrapper.eq("stock", dto.getOnOff());

        //封装分页参数
        Page<Product> pageParam = new Page<>(pageNum,size);

        //调用分页方法
        page(pageParam,wrapper);

        return pageParam;


    }

    @Override
    public int getProductAdd(Product product) {
        product.setInventory("");//上架库存
        product.setOnOff(0);//是否上架
        product.setStatus("C001-1");//是否删除


        return  productMapper.insert(product);
    }

    @Override
    public List<StockDetails> getSelStockDetails(String id) {
        QueryWrapper<StockDetails> qw = new QueryWrapper<>();
               qw.eq("product_id",id);

        List<StockDetails> stockDetailsList = stockDetailsMapper.selectList(qw);


//        int countList = 0;
//        if (stockDetailsList == null)
//            return null;
//
//        for (StockDetails stockDetailsOne:  stockDetailsList) {
//               int countOne = (int) stockDetailsOne.getRealAmount();
//
//               countList += countOne;
//
//        }

        return stockDetailsList;
    }

    @Override
    public Page<Product> queryByPage(Long pageNum, Long pageSize) {
        QueryWrapper<Product> wrapper = new QueryWrapper<Product>();

         Page<Product> page = new Page<>(pageNum, pageSize);

        return productMapper.selectPage(page,null);
    }


    @Override
    public int getUpdateProduct(String stock, String id) {

        return    productMapper.updateProduct(stock,id);

    }

    @Override
    public int getUpdateOnff(String id, Integer onff) {

            Product cart = new Product();
            cart.setOnOff(onff);
            QueryWrapper<Product> qw = new QueryWrapper<>();
            qw.lambda().eq(Product::getId,id);

            return productMapper.update(cart,qw);


    }



    @Override
    public Page<Product> pageListMedicinesCondition(ProductVo productVo, Long pageNum, Long pageSize) {

        //构建条件
        QueryWrapper<Product> wrapper = new QueryWrapper<>();

        String name = productVo.getName();

        if(!StringUtils.isEmpty(name))
            wrapper.like("name",name);
        //封装分页参数
        Page<Product> pageParam = new Page<>(pageNum, pageSize);

        page(pageParam,wrapper);

        return pageParam;
    }

}
